﻿using IOP.SgrA.GLSL;
using IOP.SgrA.SilkNet.Vulkan;
using IOP.SgrA.SilkNet.Vulkan.Scripted;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VkSample1212
{
    [ShaderVersion("460")]
    [GLSLExtension("GL_ARB_separate_shader_objects : enable")]
    [GLSLExtension("GL_ARB_shading_language_420pack : enable")]
    public partial class CommonFrag : GLSLFrag
    {
        [SampledImage(1, 0, DescriptorSetTarget.RenderObject)]
        public sampler3D sTexture { get; set; }
        [ShaderInput(0)]
        public vec3 vPosition {  get; set; }
        [ShaderInput(1)]
        public vec4 inLightQD {  get; set; }
        [FragOutput(0, false)]
        public vec4 outColor {  get; set; }

        public override void main()
        {
            vec4 lightWood = new vec4(0.6f, 0.3f, 0.1f, 1.0f); //浅色木纹颜色
            vec4 darkWood = new vec4(0.4f, 0.2f, 0.07f, 1.0f); //深色木纹颜色
            //根据片元的位置折算出3D纹理坐标--3D噪声纹理的索引
            vec3 texCoor = new vec3(((vPosition.x / 0.52f) + 1.0f) / 2.0f, vPosition.y / 0.4f, ((vPosition.z / 0.52f) + 1.0f) / 2.0f);
            vec4 noiseVec = texture(sTexture, texCoor); //进行3D纹理采样
            //计算受3D柏林噪声纹理采样值影响的位置
            vec3 location = vPosition + noiseVec.rgb * 0.05f;
            //计算离平面中心点的距离
            float dis = distance(location.xz, new vec2(0, 0));
            //年轮的频率
            dis *= 2.0f;//控制圆环的多少
            //计算两种木纹颜色的混合因子
            float r = fract(dis + noiseVec.r + noiseVec.g * 0.5f + noiseVec.b * 0.25f + noiseVec.a * 0.125f) * 2.0f;
            if (r > 1.0) r = 2.0f - r;
            //进行两种木纹颜色的混合
            vec4 color = mix(lightWood, darkWood, r);
            //再次计算调整因子
            r = fract((location.y + location.x + location.z) * 25.0f + 0.5f);
            noiseVec.a *= r;//修改噪声值
            //根据调整因子调整颜色
            if (r < 0.3) color += lightWood * 1.0f * noiseVec.a;
            else color -= lightWood * 0.02f * noiseVec.a;
            //给此片元颜色值
            outColor = color * inLightQD;
            //outColor = new vec4(location, 1.0f);
        }
    }
}
